package ru.dc.gui.ncurses;

import org.apache.commons.io.FileUtils;
import ru.dc.gui.UsersStore;
import ru.dc.object.HubEventListener;
import ru.dc.object.events.hub.*;

import javax.swing.*;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 12.10.2010
 * Time: 10:19:56
 * </pre>
 */
public class HubWindowConnectionListener implements HubEventListener {

    private HubWindow hubWindow;

    public HubWindowConnectionListener(HubWindow hubWindow) {
        this.hubWindow = hubWindow;
    }

    public void onEvent(HubEvent e) {
        if (e instanceof ChatEvent) {
            final ChatEvent chat = (ChatEvent) e;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    hubWindow.getMessages().append(chat.getNick()+": "+chat.getMessage()+"\n");
                }
            });
        }
        else if (e instanceof UserEntered) {
            final UserEntered myINFO = (UserEntered) e;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    UsersStore model = (UsersStore) hubWindow.getUsersList().getModel();
                    model.addUser(myINFO.getUserInfo());
                    hubWindow.getUsersList().validate();
                }
            });
        }
        else if (e instanceof UserLeaved) {
            final UserLeaved quit = (UserLeaved) e;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    UsersListModel model = (UsersListModel) hubWindow.getUsersList().getModel();
                    model.removeUser(quit.getNick());
                }
            });
        }
        else if (e instanceof TopicChanged) {
            final TopicChanged hubTopic = (TopicChanged) e;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    hubWindow.getTopic().setText(hubTopic.getTopic());
                }
            });
        }


        else if (e instanceof RecievedNickList) {
            final RecievedNickList nicks = (RecievedNickList) e;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    UsersListModel model = (UsersListModel) hubWindow.getUsersList().getModel();
                    //model.addUsers(Arrays.asList(nicks.getUsers()));
                }
            });
        }

        else if (e instanceof SearchResultRecieved) {
            SearchResultRecieved sr = (SearchResultRecieved) e;
            final StringBuilder s = new StringBuilder("Found: " + sr.getFilename());
            if (!sr.isDirectory()) {
                s.append(" (").append(FileUtils.byteCountToDisplaySize(sr.getSize())).append(")");
            }
            s.append("\n");
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    hubWindow.getMessages().append(s.toString());
                }
            });
        }
    }
}
